﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;
public partial class AdminX_Company_OfficeMan : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Alert1.HideMessage();
        //        Page.Title = MultipleLanguage.OWNSITE + " - " + MultipleLanguage.ADV.ToLower();
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ShowMess();
            this.BindDataToLanguageDropDown();
            this.BindDataToGrid(1);
        }
    }
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();

        // Bind Data To Language
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #region Paging & Message

    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage];
        btnAddNew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage];
        btnDepartment.Text = MultipleLanguage.Company_DepartmentManagement[nDefaultLanguage];
    }
    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
       
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

  

    #region Grid Processing
    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() -1;
        
        ContactOffice obj = new ContactOffice();
        DataTable dtb = new DataTable();
        dtb = obj.Search();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage] + dtb.Rows.Count + MultipleLanguage.LBL_FOUND[nDefaultLanguage];

        dtgAdv.DataSource = dtb;
        dtgAdv.DataBind();
    }

    protected void dtgAdv_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblOffice = (Label)e.Item.FindControl("lblOffice");
            Label lblAddress = (Label)e.Item.FindControl("lblAddress");
            Label lblTel = (Label)e.Item.FindControl("lblTel");
            Label lblFax = (Label)e.Item.FindControl("lblFax");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");
          
           
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");


            lblOffice.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Name1"));
            lblAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Address1"));
          lblTel.Text  = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Tel1"));
            lblFax.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Fax1"));
            lblEmail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ContactOfficeID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLang]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ContactOfficeID"));
        }
    }

    protected void dtgAdv_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        ContactOffice obj = new ContactOffice();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = obj.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLang]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLang]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY_ADD  + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

          
        }
    }

    #endregion

    #region Click Events
  
    //protected void btnAddnew_Click1(object sender, EventArgs e)
    //{
    //    string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
    //    Response.Redirect(sAdd);
    //}
    #endregion
    protected void btnLiveSupport_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LIVESUPPORT;
        Response.Redirect(sAdd);
    }
    //protected void btnHotline_Click(object sender, EventArgs e)
    //{
    //    string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_HOTLINE;
    //    Response.Redirect(sAdd);
    //}
    protected void btnDepartment_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_DEPARTMENT ;
        Response.Redirect(sAdd);
    }
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_COMPANY_ADD;
        Response.Redirect(sAdd);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }
}